package dao

import (
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"sync"
	"web-app/pkg/db"
)

var (
	lazySingleton *gorm.DB
	once          = &sync.Once{}
)

func GetDao(cfg db.Config) *gorm.DB {
	if lazySingleton == nil {
		once.Do(func() {
			lazySingleton, _ = db.Open(cfg,
				db.WithIgnoreRelationshipsWhenMigrating(true),
				db.WithNamingStrategy(db.Namer{schema.NamingStrategy{ //表命名规则，gorm默认将结构体加下划线转小写，并加s取复数作为表名
					TablePrefix:   "m_", //添加全局表名前缀
					SingularTable: true, //表名取单数（不加s或es）
				}}))
		})
	}
	return lazySingleton
}
